package com.shundr.shipper.common.dao;

import java.sql.SQLException;
import java.util.List;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.UpdateBuilder;
import com.shundr.shipper.common.model.SyncDataTime;
import com.shundr.shipper.frame.db.LDaoManager;

/**
 * 基础模块的数据DAO 
 * @author larry
 *
 */
public class SyncDataDao {
	
	private Dao<SyncDataTime, Integer> syncDao;
	
	public SyncDataDao() {
		syncDao = LDaoManager.createDao(SyncDataTime.class);
	}
	/**
	 * 根据类型获取
	 * @param type
	 * @return
	 */
	public String getTimeByType(String type){
		String defaultValue = "1970-07-01 00:00:00";
		try {
			List<SyncDataTime> list = syncDao.queryForEq("type", type);
			if(list.size() > 0){
				return list.get(0).getTime();
			}
			return defaultValue;
		} catch (SQLException e) {
			e.printStackTrace();
			return defaultValue;
		}
	}
	
	/**
	 * 更新对应类型时间
	 * @param type
	 * @param time
	 */
	public void updateSyncByType(String type,String time){
		try {
			UpdateBuilder<SyncDataTime, Integer>updateBuilder = syncDao.updateBuilder();
			updateBuilder.where().eq("type", type);
			updateBuilder.updateColumnValue("time", time);
			if(updateBuilder.update() == 0){
				SyncDataTime dataTime = new SyncDataTime();
				dataTime.setTime(time);
				dataTime.setType(type);
				syncDao.create(dataTime);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
}
